package hongcaosp.app.android.user.settings.safety.update;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import hongcaosp.app.android.R;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.user.settings.safety.update.iview.UpdateByCodeIView;
import hongcaosp.app.android.user.settings.safety.update.presenter.UpdateByCodePresenter;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.utils.TaskPool;

public class UpdateByCodeFragment extends BaseFragment implements UpdateByCodeIView {

    private TextView tv_hint,tv_code;
    private EditText et_code_one,et_code_two,et_code_three,et_code_four;

    private UpdateByCodePresenter presenter;

    private int time = 60;

    @Override
    public void initView() {
        presenter = new UpdateByCodePresenter(this);
        tv_hint = findViewById(R.id.tv_hint);
        tv_code = findViewById(R.id.tv_code);
        et_code_one = findViewById(R.id.et_code_one);
        et_code_two = findViewById(R.id.et_code_two);
        et_code_three = findViewById(R.id.et_code_three);
        et_code_four = findViewById(R.id.et_code_four);

        tv_hint.setText("已发送至" + UserToken.getDefault().getUser().getMobile());
        presenter.sendCode(UserToken.getDefault().getUser().getMobile());

        et_code_one.requestFocus();
        et_code_one.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    et_code_two.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_code_two.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    et_code_three.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_code_three.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    et_code_four.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_code_four.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    String code = et_code_one.getText().toString() + et_code_two.getText().toString() + et_code_three.getText().toString() + et_code_four.getText().toString();
                    presenter.check(code);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_update_by_code;
    }

    @Override
    public void sendCodeOver() {
        if (time <= 0) {
            time = 60;
            tv_code.setText("重新发送");
            tv_code.setEnabled(true);
            return;
        }
        TaskPool.myPool().postTaskOnMainDelay(new Runnable() {
            @Override
            public void run() {
                time --;
                tv_code.setText("重新发送(" + time +  ")");
                tv_code.setEnabled(false);
                sendCodeOver();
            }
        },1000);
    }

    @Override
    public void checkOver() {
        ((UpdatePasswordActivity)getActivity()).updatePasswordFragment();
    }
}
